package com.beilang.hotline.report.manager.impl;

import com.beilang.base.codelookup.manager.CodeLookupMgr;
import com.beilang.base.util.StringUtil;
import com.beilang.base.util.CodeConstant;
import com.beilang.base.util.ReportUtil;
import com.beilang.base.util.Constant;
import com.beilang.hotline.person.model.CompanyPersonModel;
import com.beilang.hotline.person.to.SearchTO;
import com.beilang.hotline.person.dao.PersonDAO;
import com.beilang.hotline.report.manager.ReportMgr;
import jxl.Workbook;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.format.Colour;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WriteException;

import java.io.File;
import java.util.*;

public class ReportMgrImpl implements ReportMgr{

    public static final String SHEETNAME = "企业联系人";

    private CodeLookupMgr codeLookupMgr;
    private PersonDAO personDAO;

    public void setPersonDAO(PersonDAO personDAO) {
        this.personDAO = personDAO;
    }

    public void setCodeLookupMgr(CodeLookupMgr codeLookupMgr) {
		this.codeLookupMgr = codeLookupMgr;
	}

	public byte[] noTransactionGenerateContactReport(SearchTO searchTO)
	{
            byte[] buf = new byte[0];
            String fileName="";
			 try {
                 fileName = getRandomFileName();
                 File file = new File(Constant.FILEPATH+fileName);

				 jxl.write.WritableWorkbook wwb = Workbook.createWorkbook(file);
				 jxl.write.WritableSheet ws = wwb.createSheet(SHEETNAME, 0);
				 writeContactExcelHeader(ws);
                 //personMgr.getPerson(new Long(1));
                 List list = personDAO.searchCompanyPersonList(searchTO);

                 if (list!=null&&!list.isEmpty())
                 {
                     Iterator itor  = list.iterator();
                     int row=1;
                     while (itor.hasNext())
                     {
                         CompanyPersonModel companyPersonModel = (CompanyPersonModel)itor.next();
                          writeContactExcelDetail(splitCompanyPersonModel(companyPersonModel),ws,row);
                          row++;
                     }
                 }

				 formatSheet(ws);

				 wwb.write();
		         wwb.close();
                 ReportUtil reportUtil = new ReportUtil();
                 buf = reportUtil.readByteArrayOfSpecifiedFile(Constant.FILEPATH+fileName);
                 if (file.exists())
                 {
                     file.delete();
                 }
            } catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
            return buf;

	}

    private  CompanyPersonModel splitCompanyPersonModel(CompanyPersonModel companyPersonModel)
    {
        if (companyPersonModel!=null)
        {
            if (!StringUtil.isEmpty(companyPersonModel.getCompanyName()))
            {
                companyPersonModel.setCompanyName(StringUtil.splitString(companyPersonModel.getCompanyName()));
            }

            if (!StringUtil.isEmpty(companyPersonModel.getPersonName()))
            {
                companyPersonModel.setPersonName(StringUtil.splitString(companyPersonModel.getPersonName()));
            }
        }

         return companyPersonModel;
    }
//    public static void deletePastFile() {
//         String pefix=getRandomFileNamePefix();
//         File file = new File(FILEPATH);
//
//         if (file.isDirectory())
//         {
//             File[] fileList = file.listFiles();
//             for (int i=0;i<fileList.length;i++)
//             {
//                 File filexx = fileList[i];
//                 if((filexx.getName().indexOf(pefix))==-1)
//                 {
//                      filexx.delete();
//                 }
//             }
//         }
//    }
	 public static String getRandomFileName() {
	        Date cur = new Date();
	        Calendar cld = Calendar.getInstance();
	        cld.setTime(cur);
	        int mth = cld.get(Calendar.MONTH) + 1;
	        String nowStr = cld.get(Calendar.YEAR) + "" + mth + cld.get(Calendar.DATE) + cld.get(Calendar.HOUR) + cld.get(Calendar.MINUTE) + cld.get(Calendar.SECOND);
	        Random rdm = new Random();
	        String rdmNum = String.valueOf(rdm.nextInt());
	        return nowStr + rdmNum + Constant.EXCEL_EXTENSION;
	    }
      public static String getRandomFileNamePefix() {
            Date cur = new Date();
	        Calendar cld = Calendar.getInstance();
	        cld.setTime(cur);
	        int mth = cld.get(Calendar.MONTH) + 1;
	        String nowStr = cld.get(Calendar.YEAR) + "" + mth + cld.get(Calendar.DATE);
	        return nowStr;
      }

	  private  void writeContactExcelHeader(jxl.write.WritableSheet ws) {
	        try {
            jxl.write.Label label18 = new jxl.write.Label(0, 0, "序号", getCell(true));
            jxl.write.Label label1 = new jxl.write.Label(1, 0, "企业名称", getCell(true));
            jxl.write.Label label2 = new jxl.write.Label(2, 0, "企业所在关区", getCell(true));
            jxl.write.Label label3 = new jxl.write.Label(3, 0, "企业代码", getCell(true));
            jxl.write.Label label4 = new jxl.write.Label(4, 0, "通讯IP", getCell(true));
            jxl.write.Label label5 = new jxl.write.Label(5, 0, "企业所有账册号", getCell(true));
            jxl.write.Label label6 = new jxl.write.Label(6, 0, "企业服务级别", getCell(true));
            jxl.write.Label label7 = new jxl.write.Label(7, 0, "企业地址", getCell(true));
            jxl.write.Label label8 = new jxl.write.Label(8, 0, "企业备注", getCell(true));
            jxl.write.Label label9 = new jxl.write.Label(9, 0, "企业MQ号", getCell(true));
            jxl.write.Label label10 = new jxl.write.Label(10, 0, "姓名", getCell(true));
            jxl.write.Label label11 = new jxl.write.Label(11, 0, "性别", getCell(true));
            jxl.write.Label label12 = new jxl.write.Label(12, 0, "固定电话", getCell(true));
            jxl.write.Label label13 = new jxl.write.Label(13, 0, "移动电话", getCell(true));
            jxl.write.Label label14 = new jxl.write.Label(14, 0, "信箱", getCell(true));
            jxl.write.Label label15 = new jxl.write.Label(15, 0, "系统联系人", getCell(true));
            jxl.write.Label label16 = new jxl.write.Label(16, 0, "电话回访", getCell(true));
            jxl.write.Label label17 = new jxl.write.Label(17, 0, "满意度调查", getCell(true));

	            ws.addCell(label1);
	            ws.addCell(label2);
	            ws.addCell(label3);
	            ws.addCell(label4);
	            ws.addCell(label5);
	            ws.addCell(label6);
	            ws.addCell(label7);
	            ws.addCell(label8);
	            ws.addCell(label9);
	            ws.addCell(label10);
	            ws.addCell(label11);
	            ws.addCell(label12);
	            ws.addCell(label13);
	            ws.addCell(label14);
	            ws.addCell(label15);
	            ws.addCell(label16);
	            ws.addCell(label17);
                ws.addCell(label18);
	        } catch (WriteException e) {
	            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
	        }

	    }

	  private  void writeContactExcelDetail(CompanyPersonModel companyPersonModel,jxl.write.WritableSheet ws,int row) {
	        try {
                jxl.write.Label label18 = new jxl.write.Label(0,row,String.valueOf(row), getCell(false));
                jxl.write.Label label1 = new jxl.write.Label(1, row, StringUtil.deNull(companyPersonModel.getCompanyName()), getCell(false));
	            String customarea=codeLookupMgr.getCodeDesc(CodeConstant.CODE_CUSTOM_AREA_LIST, StringUtil.deNull(companyPersonModel.getCustomArea()));
                customarea = StringUtil.splitString(customarea);
                jxl.write.Label label2 = new jxl.write.Label(2, row, StringUtil.deNull(customarea), getCell(false));
	            jxl.write.Label label3 = new jxl.write.Label(3, row, StringUtil.deNull(companyPersonModel.getCompanyCode()), getCell(false));
	            jxl.write.Label label4 = new jxl.write.Label(4, row, StringUtil.deNull(companyPersonModel.getContactIp()), getCell(false));
	            jxl.write.Label label5 = new jxl.write.Label(5, row, StringUtil.deNull(companyPersonModel.getCompanyNumber()), getCell(false));
	            String serviceLevel=codeLookupMgr.getCodeDesc(CodeConstant.CODE_SERVICE_LEVEL_LIST, StringUtil.deNull(companyPersonModel.getCompanyServiceLevel()));
	            jxl.write.Label label6 = new jxl.write.Label(6, row, StringUtil.deNull(serviceLevel), getCell(false));
	            jxl.write.Label label7 = new jxl.write.Label(7, row, StringUtil.deNull(companyPersonModel.getCompanyAddress()), getCell(false));
	            jxl.write.Label label8 = new jxl.write.Label(8, row, StringUtil.deNull(companyPersonModel.getCompanyRemarks()), getCell(false));
	            jxl.write.Label label9 = new jxl.write.Label(9, row, StringUtil.deNull(companyPersonModel.getCompanyMq()), getCell(false));
	            jxl.write.Label label10 = new jxl.write.Label(10, row, StringUtil.deNull(companyPersonModel.getPersonName()), getCell(false));
	            String sex=codeLookupMgr.getCodeDesc(CodeConstant.CODE_SEX_LIST, StringUtil.deNull(companyPersonModel.getSex()));
	            jxl.write.Label label11 = new jxl.write.Label(11, row,  StringUtil.deNull(sex), getCell(false));
	            jxl.write.Label label12 = new jxl.write.Label(12, row, getNumber(companyPersonModel.getTelNo(),companyPersonModel.getTelExt()), getCell(false));
	            jxl.write.Label label13 = new jxl.write.Label(13, row,  StringUtil.deNull(companyPersonModel.getHandPhone()), getCell(false));
	            jxl.write.Label label14 = new jxl.write.Label(14, row, StringUtil.deNull(companyPersonModel.getEmail()), getCell(false));
	            jxl.write.Label label15 = new jxl.write.Label(15, row,getAccessSystemString(companyPersonModel.getAccessSystemList()), getCell(false));
	            jxl.write.Label label16 = new jxl.write.Label(16, row, getBooleanDesc(companyPersonModel.isTeleBackInd()), getCell(false));
	            jxl.write.Label label17 = new jxl.write.Label(17, row, getBooleanDesc(companyPersonModel.isSurveyInd()), getCell(false));

	            ws.addCell(label1);
	            ws.addCell(label2);
	            ws.addCell(label3);
	            ws.addCell(label4);
	            ws.addCell(label5);
	            ws.addCell(label6);
	            ws.addCell(label7);
	            ws.addCell(label8);
	            ws.addCell(label9);
	            ws.addCell(label10);
	            ws.addCell(label11);
	            ws.addCell(label12);
	            ws.addCell(label13);
	            ws.addCell(label14);
	            ws.addCell(label15);
	            ws.addCell(label16);
	            ws.addCell(label17);
                ws.addCell(label18);
            } catch (WriteException e) {
	            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
	        }

	    }
	  public String getAccessSystemString(List list) {
		  String result="";
		  if (list!=null&&!list.isEmpty())
		  {
			Iterator itor = (Iterator)list.iterator();
			int i=0;
			while (itor.hasNext())
			{
				String s = (String)itor.next();
	            String sDesc=codeLookupMgr.getCodeDesc(CodeConstant.SYS_NAME, StringUtil.deNull(s));
				if (i==0)
				{
					result=sDesc;
				}else
				{
					result=result+","+sDesc;
				}
				i++;
			}

		  }
		  return result;
	  }
	  public String getBooleanDesc(boolean bln) {
		  if (bln)
		  {
			  return Constant.YES_DESC;
		  }else
		  {
			  return Constant.NO_DESC;
		  }

	  }
	  public String getNumber(String telNo,String telExt) {

		 if ( !StringUtil.isEmpty(telNo)&&!StringUtil.isEmpty(telExt))
		 {
			return telNo+ "-"+telExt;
		 }
		 if ( StringUtil.isEmpty(telNo)&&!StringUtil.isEmpty(telExt))
		 {
			return telExt;
		 }
		 if ( !StringUtil.isEmpty(telNo)&&StringUtil.isEmpty(telExt))
		 {
			return telNo;
		 }

		 return "";
	  }
	   public WritableCellFormat getCell(boolean isBold) {

            WritableFont font=null;
          if (isBold)
          {
               font  = new WritableFont(WritableFont.TIMES, 12, WritableFont.BOLD);
          }else
          {
               font  = new WritableFont(WritableFont.TIMES, 12, WritableFont.NO_BOLD);
          }


            try {
	            font.setColour(Colour.BLACK);
	        } catch (WriteException e1) {
	            e1.printStackTrace();
	        }
	        WritableCellFormat format = new WritableCellFormat(font);
	        try {
	            format.setAlignment(jxl.format.Alignment.LEFT);
	            format.setVerticalAlignment(jxl.format.VerticalAlignment.TOP);
	            format.setBorder(Border.ALL, BorderLineStyle.THIN, Colour.BLACK);
	        } catch (WriteException e) {
	            e.printStackTrace();
	        }
	        return format;
	    }
	  private static void formatSheet(jxl.write.WritableSheet ws) {
	        for (int i = 0; i < 17; i++) {
	                ws.setColumnView(i, 20);
	        }
	    }


}